package jf.collections;

import static jf.collections.CollectionF.*;
import static jf.collections.Tuple.asT;
import jf.closure.F1B;
import jf.closure.F2;
import jf.closure.F1;

import java.util.List;

/**
 * @author alex
 * @date 09.05.2009
 */
public class SimpleUsages {
	public static void main(String[] args) {
		System.out.println("Fibonachi numbers");
		for (int i : CollectionF.intArray(1, 1, 2, 3, 5, 8)) {
			System.out.println(">" + i);
		}

		System.out.println("Fibonachi numbers v2");
		for (int i : intArray(1, 1, 2, 3, 5, 8)) {
			System.out.println(">" + i);
		}


		List<String> participants = asList("John", "Mary", "Alex");
		System.out.println("Participants are " + participants);

		//this list will be immutable
		List<String> immutableList = immutable(asList("Test", "Test2", "Test3"));
		//But for this, you can use:
		List<String> immutableList2 = asIList("Test", "Test2", "Test3");

		ListX<Integer> list = asList(1, 2, 3, 4, 5, 6, 8, 9, 10);

		list = asList(1, 5, 2, 3, 4, 6, 9, 0, 7, 8, 11, 15, 13, 12, 14);
		Integer sum = list.sort().takeLeft(10).filter(new F1B<Integer>() {
			public boolean apply(Integer a) {
				return a % 2 == 0;
			}
		}).foldLeft(0, new F2<Integer, Integer, Integer>() {
			@Override
			public Integer apply(Integer a, Integer b) {
				return a + b;
			}
		});
		System.out.println("sum: " + sum);

		ListX<Double> doubles = newList();
		while (doubles.size() < 10) doubles.add(Math.random());
		doubles = doubles.sort();
		Double d = doubles.foldLeft(doubles.head(), new F2<Double, Double, Double>() {
			public Double apply(Double a, Double b) {
				return (a + b)/2;
			}
		});
		System.out.println("Average : " + d);

		ListX<Character> sdd = asList('A', 'B');
		ListX<String> probPalindromes = asList("ABBA", "AbBA", "PPSE", "IIIOOP");
		ListX<String> palindromes = probPalindromes.zip(new F1<String, ListX<Character>>() {
			public ListX<Character> apply(String s) {
				return asList(s.toUpperCase().toCharArray());
			}
		}).filter(new F1B<Tuple2<String, ListX<Character>>>() {
			public boolean apply(Tuple2<String, ListX<Character>> p) {
				return p._2.equals(p._2.reverse());
			}
		}).map(new F1<Tuple2<String, ListX<Character>>, String>() {
			public String apply(Tuple2<String, ListX<Character>> p) {
				return p._1;
			}
		});
		System.out.println("Palindromes are: " + palindromes);
	}
}
